package com.listen.service.DB;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.listen.common.HttpClientProxy;
import com.listen.model.User;
import com.listen.service.ShuiliService;
import com.listen.vo.ClassParmsVO;
import com.listen.vo.HeadParmsVO;
//水利继续教育——五大员
public class ShuiliServiceImpl implements ShuiliService {
    private static final Logger log = LoggerFactory.getLogger(ShuiliServiceImpl.class);
    private ConcurrentHashMap<String, Object>  resultMap = null;
    private HttpClientProxy instance = new HttpClientProxy();
    private String proxyInfo = null;
    private CloseableHttpResponse response = null;
    private CloseableHttpClient httpClient = null;
    private String examInfo = null;
    private String courseInfo = null;
    private String userInfo = null;
    private String kechengParam1=null;
    private String kechengParam2=null;
    private String htmlUrl = null;
    
    public static void main(String args[]) throws InterruptedException{
        ShuiliServiceImpl shuiliService = new ShuiliServiceImpl();
        String codes = "362322199710051531";
        String[] code = codes.split(",");
        for(int i = 0; i < code.length;i++) {
        	User user = new User();
            user.setCode(code[i]);
            user.setPassword(code[i].substring(0, 10));
            user.setAddress("http://www.sljxjy.cn");
            shuiliService.lisetn(user,null);
        }
    }

    //听课业务
    public Map<String,Object> lisetn(User user, String ip){
        this.proxyInfo = ip;
        ClassParmsVO vo = new ClassParmsVO();
        resultMap = new ConcurrentHashMap<>();
        resultMap.put("successFlag", "0");
        resultMap.put("dayNum", "0");
        resultMap.put("addNum", "0");
        toLoginPage(vo,user);
        return resultMap;
    }

    //根据url跳转到具体专业登录页面，并从页面抓取登录所需相关参数
    private void toLoginPage(ClassParmsVO vo,User user){
        String httpUrl = user.getAddress();
        if("".equals(httpUrl)){
            print("水利五大员登录页面url为空");
        }else{
            try {
                response = instance.sendHttpGet(httpClient,httpUrl, proxyInfo);
                int state = response.getStatusLine().getStatusCode();
                if(state == 200 || state == 301){
                    HttpEntity entity = response.getEntity();
                    String html = EntityUtils.toString(entity);
                    Document document = Jsoup.parse(html);
                    Element parm2 = document.getElementById("__EVENTVALIDATION");
                    vo.set__eventvalidation(parm2.attr("value"));
                    Element parm3 = document.getElementById("__VIEWSTATE");
                    vo.set__viewstate(parm3.attr("value"));
                    login(vo,user);
                }else{
                    print(user.getZyName()+"-"+user.getCode()+"-解析页面参数失败");
                }
            } catch (Exception e) {
                printError(user.getZyName()+"-"+user.getCode()+"-解析页面参数异常",e);
            } finally{
                instance.closeResource(response,httpClient);
            }
        }
    }
    //http://www.sljls.cn/Home.aspx
    private void login(ClassParmsVO vo,User user) throws Exception{
  		  Map<String, String> maps = new HashMap<String, String>();
  		  String psw = user.getPassword();
	      maps.put("__VIEWSTATE", vo.get__viewstate()); 
	      maps.put("__EVENTVALIDATION",vo.get__eventvalidation());
  		  maps.put("username",user.getCode());
  		  maps.put("psw",psw);
  		  maps.put("Button1", "个人登录");
	      String loginUrl = user.getAddress() + "/Home.aspx";
	      user.setReferer(user.getAddress() +"/home/UserMain.aspx");
          try {
          	HeadParmsVO head = new HeadParmsVO();
          	head.setReferer("http://www.sljls.cn/");
  	        head.setAccept("image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, */*");
  			head.setContentType("application/x-www-form-urlencoded");
  			response = instance.sendHttpPost(httpClient, head, loginUrl, maps, proxyInfo);
          	int state = response.getStatusLine().getStatusCode();
          	vo.setLoginState(state);
          	if(state==302){
          		Header[] hearders = response.getHeaders("Set-Cookie");
          		//String location = response.getHeaders("Location")[0].getValue();
          		if(hearders != null && hearders.length > 0) {
          			String cookies = hearders[0].getValue().split(";")[0];
          			user.setCookie(cookies);
					userGo(user);
          			//userMain(vo,user);
          		}
          	}else{
          		print(user.getZyName()+"-"+user.getCode()+"-登录失败--->"+state);
          	}
  		} catch (Exception e) {
  			printError(user.getZyName()+"-"+user.getCode()+"-登录异常--->",e);
  		}finally{
  			instance.closeResource(response,httpClient);
  		}
  	}
    
    //根据url跳转到具体专业登录页面，并从页面抓取登录所需相关参数
    private void userMain(ClassParmsVO vo,User user){
    	HeadParmsVO heads = new HeadParmsVO();
	    heads.setAccept("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
	    heads.setAcceptEncoding("gzip, deflate");
	    heads.setAcceptLanguage("zh-CN,zh;q=0.9,en;q=0.8");
	    heads.setCacheControl("max-age=0");
	    heads.setConnection("keep-alive");
	    heads.setHost("www.sljls.cn");
	    heads.setUpgradeInsecureRequests("1");
	    heads.setCookie(user.getCookie());
	    heads.setReferer(user.getReferer());
    	String httpUrl = user.getAddress() +"/home/UserMain.aspx";
    	user.setReferer(httpUrl);
    	try {
    		response = instance.sendHttpGet(httpClient, heads, httpUrl, proxyInfo);
            int state = response.getStatusLine().getStatusCode();
            if(state == 200){
                HttpEntity entity = response.getEntity();
                String html = EntityUtils.toString(entity);
                Document document = Jsoup.parse(html);
                Element parm1 = document.getElementById("__VIEWSTATE");
                Element parm2 = document.getElementById("__EVENTVALIDATION");
                String __VIEWSTATE = parm1.attr("value");
                String __EVENTVALIDATION = parm2.attr("value");
                userGo(user);
            }else{
                print(user.getZyName()+"-"+user.getCode()+"-userMain页面解析失败");
            }
        } catch (Exception e) {
            printError(user.getZyName()+"-"+user.getCode()+"-userMain页面解析异常",e);
        } 
    }
    
    public void userGo(User user) {
    	String userGoUrl = user.getAddress() +"/home/UserGo.aspx?id=5";
    	HeadParmsVO heads = new HeadParmsVO();
	    heads.setAccept("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
	    heads.setAcceptEncoding("gzip, deflate");
	    heads.setAcceptLanguage("zh-CN,zh;q=0.9,en;q=0.8");
	    heads.setProxyConnection("keep-alive");
	    heads.setUpgradeInsecureRequests("1");
	    heads.setCookie(user.getCookie());
	    heads.setHost("www.sljxjy.cn");
    	heads.setReferer("http://www.sljxjy.cn/home/UserMain.aspx");
    	user.setReferer(userGoUrl);
    	try {
    		response = instance.sendHttpGet(httpClient,heads,userGoUrl,proxyInfo);
            int state = response.getStatusLine().getStatusCode();
            if(state == 302){
                String location = response.getHeaders("Location")[0].getValue();
                user.setLocation(location);
                userLogin(user);
            }else{
                print(user.getZyName()+"-"+user.getCode()+"-userGo页面解析失败");
            }
		} catch (Exception e) {
			printError(user.getZyName()+"-"+user.getCode()+"-userGo页面解析失异常",e);
		}
    }
    public void userLogin(User user) {
    	String userLoginUrl = user.getLocation();
    	user.setAddress("http://wdy.sljxjy.cn");//重新设置网址域名
    	HeadParmsVO heads = new HeadParmsVO();
	    heads.setAccept("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
	    heads.setAcceptEncoding("gzip, deflate");
	    heads.setAcceptLanguage("zh-CN");
	    heads.setCookie(user.getCookie());
    	heads.setReferer(user.getReferer());
    	try {
    		response = instance.sendHttpGet(httpClient,userLoginUrl,proxyInfo);
            int state = response.getStatusLine().getStatusCode();
            if(state == 302){
                Header[] hearders = response.getHeaders("Set-Cookie");
          		if(hearders != null && hearders.length > 0) {
          			examInfo = hearders[0].getValue().split(";")[0];
          			courseInfo = hearders[3].getValue().split(";")[0];
          			userInfo = hearders[7].getValue().split(";")[0];
          			user.setCookie(courseInfo +";"+ userInfo);
          			//main(user);
					left(user);
          		}
            }else{
                print(user.getZyName()+"-"+user.getCode()+"-userLogin页面解析失败");
            }
		} catch (Exception e) {
			printError(user.getZyName()+"-"+user.getCode()+"-userLogin页面解析失异常",e);
		}finally {
			instance.closeResource(response,httpClient);
		}
    }
    
    //根据url跳转到具体专业登录页面，并从页面抓取登录所需相关参数
    private void main(User user){
    	HeadParmsVO heads = new HeadParmsVO();
	    heads.setAccept("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
	    heads.setAcceptEncoding("gzip, deflate");
	    heads.setAcceptLanguage("zh-CN,zh;q=0.9,en;q=0.8");
	    heads.setCacheControl("max-age=0");
	    heads.setConnection("keep-alive");
	    heads.setHost("wdy.sljxjy.cn");
	    heads.setUpgradeInsecureRequests("1");
	    heads.setCookie(user.getCookie());
	    heads.setReferer(user.getReferer());
    	String httpUrl = user.getAddress() +"/user/main.aspx";
    	user.setReferer(httpUrl);
    	try {
    		response = instance.sendHttpGet(httpClient, heads, httpUrl, proxyInfo);
            int state = response.getStatusLine().getStatusCode();
            if(state == 200){
            	left(user);
            }else{
                print(user.getZyName()+"-"+user.getCode()+"-main页面解析失败");
            }
        } catch (Exception e) {
            printError(user.getZyName()+"-"+user.getCode()+"-main页面解析异常",e);
        } finally{
            instance.closeResource(response,httpClient);
        }
    }
    
  //根据url跳转到具体专业登录页面，并从页面抓取登录所需相关参数
    private void left(User user){
    	HeadParmsVO heads = new HeadParmsVO();
	    heads.setAccept("text/html, application/xhtml+xml, image/jxr, */*");
	    heads.setAcceptEncoding("gzip, deflate");
	    heads.setAcceptLanguage("zh-CN,zh;q=0.9,en;q=0.8");
	    heads.setConnection("keep-alive");
	    heads.setHost("wdy.sljxjy.cn");
	    heads.setCookie(user.getCookie());
	    heads.setReferer(user.getAddress() + "/user/main.aspx");
    	String httpUrl = user.getAddress() + "/user/left.aspx";
    	user.setReferer(httpUrl);
    	try {
    		response = instance.sendHttpGet(httpClient, heads, httpUrl, proxyInfo);
            int state = response.getStatusLine().getStatusCode();
            if(state == 200){
            	HttpEntity entity = response.getEntity();
                String html = EntityUtils.toString(entity);
                Document document = Jsoup.parse(html);
                Elements links = document.select("a[title=资料册]");
                if(links != null && links.size() > 0) {
                	String page = links.get(0).attr("href");
                	user.setLocation(user.getAddress() +"/user/" + page);
                	kecheng(user);
                }else {
                	print(user.getZyName()+"-"+user.getCode()+"-left页面解析失败-----");
                }
            }else{
                print(user.getZyName()+"-"+user.getCode()+"-left页面解析失败");
            }
        } catch (Exception e) {
            printError(user.getZyName()+"-"+user.getCode()+"-left页面解析异常",e);
        } finally{
            instance.closeResource(response,httpClient);
        }
    }
    
    //根据url跳转到具体专业登录页面，并从页面抓取登录所需相关参数
    private void kecheng(User user){
    	HeadParmsVO heads = new HeadParmsVO();
	    heads.setAccept("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
	    heads.setAcceptEncoding("gzip, deflate");
	    heads.setAcceptLanguage("zh-CN,zh;q=0.9,en;q=0.8");
	    heads.setCacheControl("max-age=0");
	    heads.setConnection("keep-alive");
	    heads.setHost("wdy.sljxjy.cn");
	    heads.setUpgradeInsecureRequests("1");
	    heads.setCookie(user.getCookie());
	    heads.setReferer(user.getReferer());
    	String httpUrl = user.getLocation();
    	user.setReferer(httpUrl);
    	try {
    		response = instance.sendHttpGet(httpClient, heads, httpUrl, proxyInfo);
            int state = response.getStatusLine().getStatusCode();
            if(state == 200){
                HttpEntity entity = response.getEntity();
                String html = EntityUtils.toString(entity);
                Document document = Jsoup.parse(html);
                Elements links = document.select("input[value=进入学习]");
                if(links != null && links.size() == 2) {
                	String method1 = links.get(0).attr("onclick");
                	kechengParam1 = method1.substring(method1.indexOf("(")+1, method1.indexOf(")"));
                	String method2 = links.get(1).attr("onclick");
                	kechengParam2 = method2.substring(method2.indexOf("(")+1, method2.indexOf(")")).replace("\\", "");
                	learningCourse(user);
                }else {
                	print(user.getZyName()+"-"+user.getCode()+"-kecheng页面解析失败----");
                }
            }else{
                print(user.getZyName()+"-"+user.getCode()+"-kecheng页面解析失败");
            }
        } catch (Exception e) {
            printError(user.getZyName()+"-"+user.getCode()+"-kecheng页面解析异常",e);
        } finally{
            instance.closeResource(response,httpClient);
        }
    }
    
    //http://wdy.sljxjy.cn/user/Learning/LearningCourse.aspx?cid=5&curl=Course/C5
    private void learningCourse(User user) {
    	String param = kechengParam1.replaceAll("'", "");
    	String curl = param.split(",")[0];
    	String cid = param.split(",")[1];
    	String learningCourseUrl = user.getAddress() + "/user/Learning/LearningCourse.aspx?cid="+cid+"&curl="+curl;
    	HeadParmsVO heads = new HeadParmsVO();
	    heads.setAccept("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
	    heads.setAcceptEncoding("gzip, deflate");
	    heads.setAcceptLanguage("zh-CN,zh;q=0.9,en;q=0.8");
	    heads.setCacheControl("max-age=0");
	    heads.setConnection("keep-alive");
	    heads.setHost("wdy.sljxjy.cn");
	    heads.setUpgradeInsecureRequests("1");
	    heads.setCookie(user.getCookie());
	    heads.setReferer(user.getReferer());
    	user.setReferer(learningCourseUrl);
    	try {
    		response = instance.sendHttpGet(httpClient, heads, learningCourseUrl, proxyInfo);
            int state = response.getStatusLine().getStatusCode();
            if(state == 200){
                Header[] hearders = response.getHeaders("Set-Cookie");
          		if(hearders != null && hearders.length > 0) {
          			String cookie = hearders[0].getValue().split(";")[0];
          			user.setCookie(this.userInfo + ";" +cookie);
          			C5(user);
          		}
            }else{
                print(user.getZyName()+"-"+user.getCode()+"-learningCourse页面解析失败");
            }
        } catch (Exception e) {
            printError(user.getZyName()+"-"+user.getCode()+"-learningCourse页面解析异常",e);
        } finally{
            instance.closeResource(response,httpClient);
        }
    }
    
    //http://wdy.sljxjy.cn/Course/C5/
    
    private void C5(User user) {
    	String c5Url = "http://wdy.sljxjy.cn/Course/C5/";
    	response = instance.sendHttpGet(httpClient, c5Url, proxyInfo);
    	int state = response.getStatusLine().getStatusCode();
    	if(state == 200) {
    		learning3(user);
    	}
    }
    
    //http://wdy.sljxjy.cn/user/learning/learning3.aspx
    private void learning3(User user) {
    	String learning3 = user.getAddress() + "/user/learning/learning3.aspx";
    	HeadParmsVO heads = new HeadParmsVO();
	    heads.setAccept("image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, */*");
	    heads.setAcceptEncoding("gzip, deflate");
	    heads.setAcceptLanguage("zh-CN");
	    heads.setConnection("keep-alive");
	    heads.setHost("wdy.sljxjy.cn");
	    heads.setCookie(user.getCookie());
	    heads.setReferer(user.getReferer());
	    
	    user.setReferer(learning3);
    	try {
    		response = instance.sendHttpGet(httpClient, heads, learning3, proxyInfo);
            int state = response.getStatusLine().getStatusCode();
            if(state == 200){
            	mediaPlayer(user);
            }else{
                print(user.getZyName()+"-"+user.getCode()+"-learning3页面解析失败");
            }
        } catch (Exception e) {
            printError(user.getZyName()+"-"+user.getCode()+"-learning3页面解析异常",e);
        } finally{
            instance.closeResource(response,httpClient);
        }
    }
    
    //http://wdy.sljxjy.cn/Course/C5/mediaPlayer.htm?id=
    private void mediaPlayer(User user) {
    	String mediaPlayerUrl = "http://wdy.sljxjy.cn/Course/C5/mediaPlayer.htm?id=";
    	
    	user.setReferer(mediaPlayerUrl);
    	try {
    		response = instance.sendHttpGet(httpClient, mediaPlayerUrl, proxyInfo);
            int state = response.getStatusLine().getStatusCode();
            if(state == 200){
            	//ETag: "9f7d196ff341d11:0"
            	//String etag = response.getLastHeader("ETag").getValue();
            	//HttpEntity entity = response.getEntity();
                //String html = EntityUtils.toString(entity);
            	getUpStudyTime(user);
            }else{
                print(user.getZyName()+"-"+user.getCode()+"-mediaPlayer页面解析失败");
            }
        } catch (Exception e) {
            printError(user.getZyName()+"-"+user.getCode()+"-mediaPlayer页面解析异常",e);
        } finally{
            instance.closeResource(response,httpClient);
        }
    }
    //http://wdy.sljxjy.cn/user/learning/GetUpStudyTime.aspx
    private void getUpStudyTime(User user) {
    	String getUpStudyTimeUrl = user.getAddress() + "/user/learning/GetUpStudyTime.aspx";
    	HeadParmsVO heads = new HeadParmsVO();
	    heads.setAccept("*/*");
	    heads.setAcceptEncoding("gzip, deflate");
	    heads.setCacheControl("no-cache");
	    heads.setAcceptLanguage("zh-cn");
	    heads.setContentType("text/xml; charset=gb2312");
	    heads.setConnection("keep-alive");
	    heads.setHost("wdy.sljxjy.cn");
	    heads.setCookie(user.getCookie());
	    heads.setReferer(user.getReferer());
	    
	    user.setReferer(getUpStudyTimeUrl);
	    int classNum = 0;
	    log.info("开始听---"+user.getCode()+"-的课程，共15节");
	    //单机
	    /*try {
    		Thread.sleep(3 *1000);
    		response = instance.sendHttpGet(httpClient, heads, getUpStudyTimeUrl, proxyInfo);
    		int state = response.getStatusLine().getStatusCode();
    		if(state == 200){
    			HttpEntity entity = response.getEntity();
    			String result = EntityUtils.toString(entity);
    			String mediaUrl = user.getAddress() + "/Course/C5/"+result.split(";")[1];
    			classNum = Integer.parseInt(result.split(";")[0]);
    			user.setLocation(mediaUrl);
    			user.setNowNum(classNum);
    			htmlUrl = user.getAddress() + "/Course/C5/"+result.split(";")[3];
    			media(user);
    		}else{
    			print(user.getZyName()+"-"+user.getCode()+"-getUpStudyTime页面解析失败");
    		}
    	} catch (Exception e) {
    		printError(user.getZyName()+"-"+user.getCode()+"-getUpStudyTime页面解析异常",e);
    	} finally{
    		instance.closeResource(response,httpClient);
    	}*/
	    //单机不连库
	    while(classNum < 15) {
	    	try {
	    		Thread.sleep(5 *1000);
	    		response = instance.sendHttpGet(httpClient, heads, getUpStudyTimeUrl, proxyInfo);
	    		int state = response.getStatusLine().getStatusCode();
	    		if(state == 200){
	    			HttpEntity entity = response.getEntity();
	    			String result = EntityUtils.toString(entity);
	    			String mediaUrl = user.getAddress() + "/Course/C5/"+result.split(";")[1];
	    			classNum = Integer.parseInt(result.split(";")[0]);
	    			user.setLocation(mediaUrl);
	    			user.setNowNum(classNum);
	    			htmlUrl = user.getAddress() + "/Course/C5/"+result.split(";")[3];
	    			media(user);
	    		}else{
	    			print(user.getZyName()+"-"+user.getCode()+"-getUpStudyTime页面解析失败");
	    		}
	    	} catch (Exception e) {
	    		printError(user.getZyName()+"-"+user.getCode()+"-getUpStudyTime页面解析异常",e);
	    	} finally{
	    		instance.closeResource(response,httpClient);
	    	}
    	}
    }
    
    //http://wdy.sljxjy.cn/Course/C5/Media/012.rm
    private void media(User user) {
    	String mediaUrl = user.getLocation();
    	int num = user.getNowNum();
    	String rang = this.getNumRange(num);
    	HeadParmsVO heads = new HeadParmsVO();
	    heads.setAccept("*/*");
	    heads.setAcceptEncoding("gzip, deflate");
	    heads.setCacheControl("no-cache");
	    heads.setAcceptLanguage("zh-cn");
	    heads.setContentType("text/xml; charset=gb2312");
	    heads.setConnection("keep-alive");
	    heads.setHost("wdy.sljxjy.cn");
	    heads.setIfRange(rang.split(";")[0]);
	    heads.setRange(rang.split(";")[1]);
	    heads.setCookie(user.getCookie());
	    heads.setReferer(user.getReferer());
	    
	    user.setReferer(mediaUrl);
    	try {
    		response = instance.sendHttpGet(httpClient, heads, mediaUrl, proxyInfo);
            int state = response.getStatusLine().getStatusCode();
            if(state == 200){
            	courseFinish(user);
            }else{
                print(user.getZyName()+"-"+user.getCode()+"-media页面解析失败");
            }
        } catch (Exception e) {
            printError(user.getZyName()+"-"+user.getCode()+"-media页面解析异常",e);
        } finally{
            instance.closeResource(response,httpClient);
        }
    }
    
    //http://wdy.sljxjy.cn/user/Learning/CourseFinish.aspx
    private void courseFinish(User user) {
    	String courseFinishUrl = user.getAddress() + "/user/Learning/CourseFinish.aspx";
    	HeadParmsVO heads = new HeadParmsVO();
	    heads.setAccept("*/*");
	    heads.setAcceptEncoding("gzip, deflate");
	    heads.setCacheControl("no-cache");
	    heads.setAcceptLanguage("zh-cn");
	    heads.setContentType("text/xml; charset=gb2312");
	    heads.setConnection("keep-alive");
	    heads.setHost("wdy.sljxjy.cn");
	    heads.setCookie(user.getCookie());
	    heads.setReferer("http://wdy.sljxjy.cn/Course/C5/");
	    int studytime = (int)(2400+Math.random()*(3600-2400+1));//2400-3600之间的数
	    String xml = "<?xml version=\"1.0\" encoding=\"gb2312\"?>\n" + 
	    		"<Info><id>"+user.getNowNum()+"</id><studytime>"+ studytime +"</studytime><studypoint>0</studypoint><state>0</state></Info>";
	    try {
    		Thread.sleep(5 *1000);
			response = instance.sendHttpPost(httpClient, heads, courseFinishUrl,xml, proxyInfo);
            int state = response.getStatusLine().getStatusCode();
            HttpEntity entity = response.getEntity();
        	String body = EntityUtils.toString(entity);
            if(state == 200){
            	int result = Integer.parseInt(body);
            	if(result == 1) {
            		resultMap.put("successFlag", "1");
            		resultMap.put("nowNum", user.getNowNum());
            		print(user.getZyName()+"-"+user.getCode()+"-恭喜你已完成第"+user.getNowNum()+"节课程的学习");
            	}
            }else{
                print(user.getZyName()+"-"+user.getCode()+"-courseFinish页面解析失败");
            }
        } catch (Exception e) {
            printError(user.getZyName()+"-"+user.getCode()+"-courseFinish页面解析异常",e);
        } finally{
            instance.closeResource(response,httpClient);
        }
    }
    
    private String getNumRange(int num) {
    	switch(num) {
    	case 1:return  "3e4c8aef48f2cf1:0;bytes=7229-";
    	case 2:return  "031fa5648e3cd1:0;bytes=750174-";
    	case 3:return  "04519cce0e3cd1:0;bytes=216134-";
    	case 4:return  "088bda0e5cd1:0;bytes=330292-";
    	case 5:return  "0c154af21eacd1:0;bytes=5490-";
    	case 6:return  "0a2e982e5ebcd1:0;bytes=134205-";
    	case 7:return  "0f8b5e48eacd1:0;bytes=376042-";
    	case 8:return  "04bcb574beacd1:0;bytes=346130-";
    	case 9:return  "056bd64feacd1:0;bytes=89011-";
    	case 10:return  "061364956eacd1:0;bytes=44370-";
    	case 11:return  "0b08095edeacd1:0;bytes=220465-";
    	case 12:return  "0735d3f4eacd1:0;bytes=119251-";
    	case 13:return  "066d3984ebcd1:0;bytes=204688-";
    	case 14:return  "0f07cea6ebcd1:0;bytes=5491-";
    	case 15:return  "02f70d48ebcd1:0;bytes=147951-";
    	}
    	return null;
    }
    
    private void print(String msg){
        resultMap.put("msg", msg);
        log.info(msg);
    }

    private void printError(String msg,Exception e){
        msg = msg + e.getMessage();
        resultMap.put("msg", msg);
        log.info(msg,e);
    }
}
